//
//  NSStrokeFill.m
//  StrokeDemo
//
//  Created by wankang on 2/13/15.
//  Copyright (c) 2015 wankang. All rights reserved.
//

#import "NSStrokeFill.h"
#import "ios_StrokeFill.h"

@implementation NSStrokeFill
@synthesize maxWidth = _maxWidth;
@synthesize maxHeight = _maxHeight;

-(id)init
{
    if ((self = [super init]) != nil) {
        _maxWidth = 267;
        _maxHeight = 267;
        mFiller = sf_createFiller();
        if (mFiller == nil)
            return nil;
    }
    return self;
}

-(void)dealloc
{
    [self close];
}

-(void)close
{
    if (mFiller != nil) {
        sf_destroyFiller(mFiller);
        mFiller = NULL;
    }
}

-(BOOL)setContour:(NSArray *)arrContour andMiddle:(NSArray *)arrMidline
{
    if (!mFiller)
        return NO;
    int nContourPoint = (int)(arrContour!=nil ? arrContour.count : 0);
    if (nContourPoint <= 2)
        return NO;
    short *p2 = NULL;
    short *p1 = (short *)malloc(sizeof(short) * nContourPoint * 2);
    for (int m=0; m<nContourPoint; m++) {
        NSNumber *value = [arrContour objectAtIndex:m];
        p1[2 * m + 0] = (short)(value.intValue & 0xffff);
        p1[2 * m + 1] = (short)((value.intValue >> 16) & 0xffff);
    }
    int nMidPoint = (int)(arrMidline!=nil ? arrMidline.count : 0);
    if (nMidPoint > 0) {
        p2 = (short *)malloc(sizeof(short) * nMidPoint * 2);
        for (int n=0; n<nMidPoint; n++) {
            NSNumber *value = [arrMidline objectAtIndex:n];
            p2[2 * n + 0] = (short)(value.intValue & 0xffff);
            p2[2 * n + 1] = (short)((value.intValue >> 16) & 0xffff);
        }
    }
    int nRes = sf_setData(mFiller, p1, nContourPoint * 2, p2, nMidPoint * 2);
    free(p1);
    if (p2)
        free(p2);
    return (nRes != 0);
}

-(BOOL)setGuide:(NSArray *)arrGuide
{
    if (!mFiller)
        return NO;
    // for customized width/height
    sf_setMaxSize(mFiller, (int)_maxWidth, (int)_maxHeight);
    int nPoint = (int)(arrGuide!=nil ? arrGuide.count : 0);
    if (nPoint <= 0)
        return NO;
    short buf[128];
    short *p = buf;
    if (nPoint > 64)
        p = (short *)malloc(sizeof(short) * nPoint * 2);
    for (int n=0; n<nPoint; n++) {
        NSNumber *v = [arrGuide objectAtIndex:n];
        p[2 * n + 0] = (short)(v.intValue & 0xffff);
        p[2 * n + 1] = (short)((v.intValue >> 16) & 0xffff);
    }
    int nRes = sf_setGuide(mFiller, p, nPoint * 2);
    if (p != buf)
        free(p);
    return (nRes != 0);
}

-(BOOL)setStartPoint:(StrokePoint)pt
{
    if (!mFiller || pt.x<0 || pt.y<0)
        return NO;
    // for customized width/height
    sf_setMaxSize(mFiller, (int)_maxWidth, (int)_maxHeight);
    return sf_setStartXY(mFiller, pt.x, pt.y);
}

-(NSArray *)getPoints:(NSInteger)nMaxPoint
{
    if (!mFiller || nMaxPoint<=0)
        return nil;
    short buf[128];
    if (nMaxPoint > 64)
        nMaxPoint = 64;
    short *p = buf;
    int nRes = sf_getPoints(mFiller, p, (int)nMaxPoint * 2);
    if (nRes <= 0)
        return nil;
    int nCount = nRes / 2;
    NSMutableArray *res = [[NSMutableArray alloc] initWithCapacity:nCount];
    for (int n=0; n<nCount; n++) {
        int value = p[2 * n] + ( ((int)p[2 * n + 1]) << 16 );
        NSNumber *v = [NSNumber numberWithInt:value];
        [res addObject:v];
    }
    return res;
}

-(NSInteger)getPoints:(StrokePoint *)ptList withMaximumCount:(NSInteger)nMaxPoint
{
    if (!mFiller || nMaxPoint<=0)
        return 0;
    short *p = (short *)ptList;
    int nRes = sf_getPoints(mFiller, p, (int)nMaxPoint * 2);
    if (nRes <= 0)
        return 0;
    return nRes / 2;
}
@end
